﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpGenericTeach
{
    /// <summary>
    /// 出栈入栈泛型类
    /// </summary>
    public class MyStack<T>
    {
        //泛型数组
        private T[] stack; //泛型数组

        //元素位置
        private int stackIndex;

        //数据容量
        private int size;

        public MyStack(int size)
        {
            this.size = size;
            this.stack = new T[size];
            this.stackIndex = -1; //入栈开始是0，每次加1
        }

        //元素入栈
        public void Push(T item)
        {
            if(this.stackIndex >= this.size)
            {
                Console.WriteLine("栈空间已满！");
            }
            else
            {
                this.stackIndex++;
                this.stack[this.stackIndex] = item;
            }
        }

        //元素出栈
        public T Pop()
        {
            T data = this.stack[this.stackIndex];
            stackIndex--; //元素出栈后，下一个要出栈的元素索引
            return data;
        }
    }
}
